<?php
//----------------------------------------------------------------------------------------------------------
    require_once('../config.inc.php');

    require_once($config['app_dir'] . '/classes/stockplot.view.php');
    require_once($config['app_dir'] . '/classes/stockplot.data.php');
    //------------------------------------------------------------------------------------------------------
    $cached_time = 60*60*24; //time in seconds
    $width = 800;
    $height = 500;
    //------------------------------------------------------------------------------------------------------
    try {
       //---------------------------------------------------------------------------------------------------
       // checking input parameters
       //---------------------------------------------------------------------------------------------------
       $StockParams = new objStockParams();
       $id = $StockParams->GenID();
       //---------------------------------------------------------------------------------------------------
       // preparing data for graph
       //---------------------------------------------------------------------------------------------------
       $StockView = new objStockView($width, $height);
       //---------------------------------------------------------------------------------------------------
       $image =  'nforecast__' . $id . '.' . $StockView->type;
       $image_path = $config['jpgraph']['cache_dir'] . $image;
       $text = '';
       //---------------------------------------------------------------------------------------------------
       if( $cached_time>0 && file_exists($image_path) && time()-filemtime($image_path)>$cached_time )  @unlink($image);

       if( !file_exists($image_path) ) {
          $StockModel = new objStockModel($StockParams->trade, $StockParams->start, $StockParams->finish);
          $StockModel->SetNews(true);
          $StockModel->StrongInterval(true);
          $StockModel->ExactInterval(false);
          //------------------------------------------------------------------------------------------------
          // generating graph
          //------------------------------------------------------------------------------------------------
          $StockView->HideLogo();
          $StockView->HideNews(false);
          $StockView->HideCopyright();
          $StockView->SelectWeekends(false);
          $StockView->DivideWeeks(true);
          $StockView->SetDisabledVectorColors(array(3));
          $StockView->SetLegendTitles(array('darkgreen'=>'correct forecast', 'red'=>'incorrect forecast'));

          $StockView->SetData($StockModel->getData());

          $text = $StockView->RenderNews();
          $StockView->Render($image_path);
        } //if not cached
        else {
          $StockNewsTextView = new objStockNewsTextView($id);
          $text = $StockNewsTextView->Fetch();
        }
        //---------------------------------------------------------------------------------------------------
        $Template = new objTemplate();
        $Template->Assign('image', $image);
        $Template->Assign('text', $text);
        $Template->Display('nForecast.tpl');
        //---------------------------------------------------------------------------------------------------
    } catch (Exception $e) {
        echo 'Error: ',  $e->getMessage(), "\n";
    }
   //-------------------------------------------------------------------------------------------------------
?>
